package com.lulin.mall.coupon.service.impl;

import com.lulin.mall.coupon.entity.SeckillSkuRelationEntity;
import com.lulin.mall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.Query;

import com.lulin.mall.coupon.dao.SeckillSessionDao;
import com.lulin.mall.coupon.entity.SeckillSessionEntity;
import com.lulin.mall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService skuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询到未来3天秒杀活动，已经该活动对应的sku商品信息
     */
    @Override
    public List<SeckillSessionEntity> getLate3DaysSeckillSkuInfo() {
        //计算未来3天的时间
        String startTime = startTime();
        String endTime = endTime();
        //1、根据查询条件,查询到活动信息
        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime, endTime));
        //2、现在针对每个活动， 查询这个活动对应的商品。（可能关联多个商品的）
        List<SeckillSessionEntity> newList = list.stream().map(session -> {
            //根据活动场次编号，查询对应活动是sku信息
            List<SeckillSkuRelationEntity> skuRelationEntities = skuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>()
                    .eq("promotion_session_id", session.getId()));
            session.setRelationEntities(skuRelationEntities);
            return session;
        }).collect(Collectors.toList());

        return newList;
    }

    /**
     * 开始时间
     */
    private String startTime() {
        //获取当前时间
        LocalDate now = LocalDate.now();
        //加1天，开始的时间
        LocalDate startDay = now.plusDays(1);
        //获取对应的时间（最小）
        LocalTime min = LocalTime.MIN;// 00:00
        //日期和时间的组合
        LocalDateTime startTime = LocalDateTime.of(startDay, min);
        return startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 结束时间
     */
    private String endTime() {
        //获取当前时间
        LocalDate now = LocalDate.now();
        //加3天，开始的时间
        LocalDate startDay = now.plusDays(3);
        //获取对应的时间（最大）
        LocalTime max = LocalTime.MAX;//23:59:59.999999999
        //日期和时间的组合
        LocalDateTime endTime = LocalDateTime.of(startDay, max);
        return endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}
